<?php

/**
 * 按照趣炫要求签名方法
 * 服务器监控对接备份数据传送对接
 * @author yuxun
 * game_id 由趣炫分配
 * time 为unix时间戳
 * sign 为验证串
 * key  为约定密码
 */
class ServerStateController extends Controller {
	public function __construct($id,$module=null){
		parent::__construct();
	}

	
	public function beforeAction($action){
		$local_sign = $this->getSign();
// 		echo $local_sign;echo '<br/>';
// 		echo PubRequest::R('sign');exit;
		
		$sign = PubRequest::R('sign');

		if($local_sign != $sign){
			$json_arr = array('state'=>0,'msg'=>'check_sign_err' );
			ApiBaseService::doLog($_POST, $json_arr);
			exit(json_encode($json_arr));
		}
			
		return parent::beforeAction($action);
	}
	
	
	/**
	 * game_id&time&key 拼接之后进行md5取值, 中间无空格和别的特殊字符,&参加签名生成
	 * php示例 : $sign = md5($game_id.'&'.$time.'&'.$key);
	 * @return string
	 */
	protected function getSign(){
		header("Content-type:text/html;charset=utf-8");
		$key = Yii::app()->params['QX_SERVERSTATUE_KEY'];
		
		$game_id = PubRequest::R('game_id');
		$time = PubRequest::R('time');
		//$sign = $game_id.'&'.$time.'&'.$key;
		$sign = md5($game_id.'&'.$time.'&'.$key);
		
		return $sign;
	}
	
	/**
	 * http://admin.s10.q-dazzle.com:8090/api/?r=serverState/monitor
	 * game_id 30
	 * key XZLr3583Vj3Yth8t
	 * 兼容多服务器的方法(此方法需要解决mysql的only_full_group_by问题，错误代码为1055，修改my.cnf配置文件)
	 */
	public function monitor2(){
		$game_id = PubRequest::R('game_id');
		$time = PubRequest::R('time');
		
		$json_arr = ServerStateService::monitor($game_id,$time);
		ApiBaseService::doLog($_POST, $json_arr);
		exit(json_encode($json_arr));
	}
    
	/**
	 * http://admin.s10.q-dazzle.com:8090/api/?r=serverState/monitor
	 * game_id 30
	 * key XZLr3583Vj3Yth8t
	 */
	public function monitor(){
		$game_id = PubRequest::R('game_id');
		$time = PubRequest::R('time');
		
		if($game_id == ''){
			$json_arr = array('state'=>0,'msg'=>'need to parameter game_id' );
			ApiBaseService::doLog($_POST, $json_arr);
			exit(json_encode($json_arr));
		}
		
		if($game_id != 30){
			$json_arr = array('state'=>0,'msg'=>'Parameter value error game_id' );
			ApiBaseService::doLog($_POST, $json_arr);
			exit(json_encode($json_arr));
		}
		
		if($time == ''){
			$json_arr = array('state'=>0,'msg'=>'need to parameter time' );
			ApiBaseService::doLog($_POST, $json_arr);
			exit(json_encode($json_arr));
		}
		

		$modMonitordb = new ModMonitordb();
		$serverInfoList = $modMonitordb->getServerInfoList();
// 		echo '<pre>';
// 		var_dump($serverInfoList);
		
		if(count($serverInfoList) == 0){
			$json_arr = array('state'=>0,'msg'=>'no data' ); //  没有数据
			ApiBaseService::doLog($_POST, $json_arr);
			exit(json_encode($json_arr));
		}
		
		$run_bash = $serverInfoList[0]['run_bash'];
		 
		$run_bash_list = $modMonitordb->online($run_bash);
		
		$d_arr = array();
		$d_arr['ip'] = $serverInfoList[0]['ip'];
		$d_arr['load'] = $serverInfoList[0]['load'];
		$d_arr['space_all'] = $serverInfoList[0]['space_all'];
		$d_arr['space_use'] = $serverInfoList[0]['space_use'];
		$d_arr['mem_all'] = $serverInfoList[0]['mem_all'];
		$d_arr['mem_free'] = $serverInfoList[0]['mem_free'];
		$d_arr['server_num'] = $serverInfoList[0]['server_num'];
		$d_arr['online_num'] = 0;
		$d_arr['online_top_num'] = 0;
		$d_arr['online_detail'] = array();
		
		$online_num = 0;
		$online_top_num = 0;
		$r_data = array();
		foreach ($run_bash_list as $key=>$val){
			$d = array();
			$d['plat_id'] = $val['plat_id'];
			$d['server_id'] = $val['server_id'];
			$d['s_num'] = $val['s_num'];
			$d['top_num'] = $val['top_num'];
			array_push($r_data, $d);
			$online_num += $val['s_num'];
			$online_top_num += $val['top_num'];
		}
		$d_arr['online_num'] = $online_num;
		$d_arr['online_top_num'] = $online_top_num;
		$d_arr['online_detail'] = $r_data;
		
		$d_arr_arr = array();
		array_push($d_arr_arr, $d_arr);
		//array_push($d_arr_arr, $d_arr);
		
		//var_dump($d_arr_arr);		
		

		$result['state'] = true;
		if($result['state']){
			//$json_arr = array('state'=>1,'msg'=>'success','data'=>$data);			
			$json_arr = $d_arr_arr;
			ApiBaseService::doLog($_POST, $json_arr);
			exit(json_encode($json_arr));
		}else{
			$json_arr = array('state'=>0,'msg'=>'fail' );
			ApiBaseService::doLog($_POST, $json_arr);
			exit(json_encode($json_arr));
		}
	}
	
	
   

}